﻿namespace Services.Server
{
	public class ALOGSData
	{
		public static string Server = string.Empty;
		public static string Name = "AVC日志服务";

		/// <summary>
		/// 获取日志数据
		/// </summary>
		/// <param name="name"></param>
		/// <param name="start"></param>
		/// <param name="end"></param>
		/// <param name="pageIndex"></param>
		/// <param name="pageSize"></param>
		/// <param name="count"></param>
		/// <returns></returns>
		public static async Task<List<Server_LogDatas>> GetQueryLogPage(string? account, string? name, string? content, string start, string end, int pageIndex, int pageSize, RefAsync<int> count)
		{
			string path = "/Query";
			var param = new
			{
				type = "log",
				source = "knowledgegraph",
				start_time = start,
				end_time = end,
				page_index = pageIndex,
				page_size = pageSize,
				CommonType = 0,
				Name = !string.IsNullOrWhiteSpace(name) ? $"${name}" : string.Empty,
				Account = !string.IsNullOrWhiteSpace(account) ? $"${account}" : string.Empty,
				Content = !string.IsNullOrWhiteSpace(content) ? $"${content}" : string.Empty,
			};
			var res = await PostData<Server_LogQuery<List<Server_LogDatas>>>(path, param);
			if (res == null || res.result == null)
			{
				count.Value = 0;
				return new List<Server_LogDatas>();
			}
			count.Value = res.count;
			return res.result;
		}


		/// <summary>
		/// 获取登录日志数据
		/// </summary>
		/// <param name="name"></param>
		/// <param name="start"></param>
		/// <param name="end"></param>
		/// <param name="pageIndex"></param>
		/// <param name="pageSize"></param>
		/// <param name="count"></param>
		/// <returns></returns>
		public static async Task<List<Server_LoginLogData>> GetQueryLoginLogPage(string? uid, string? name, string ip, string start, string end, int pageIndex, int pageSize, RefAsync<int> count)
		{
			string path = "/Query";
			var param = new
			{
				type = "log",
				source = "knowledgegraph",
				start_time = start,
				end_time = end,
				page_index = pageIndex,
				page_size = pageSize,
				CommonType = 2,
				Name = !string.IsNullOrWhiteSpace(name) ? $"${name}" : string.Empty,
				Uid = !string.IsNullOrWhiteSpace(uid) ? $"${uid}" : string.Empty,
				Ip = !string.IsNullOrWhiteSpace(ip) ? $"${ip}" : string.Empty,
			};
			var res = await PostData<Server_LogQuery<List<Server_LoginLogData>>>(path, param);
			if (res == null || res.result == null)
			{
				count.Value = 0;
				return new List<Server_LoginLogData>();
			}
			count.Value = res.count;
			return res.result;
		}

		/// <summary>
		/// 获取事件日志数据
		/// </summary>
		/// <param name="source"></param>
		/// <param name="event_type"></param>
		/// <param name="device_id"></param>
		/// <param name="start_time"></param>
		/// <param name="end_time"></param>
		/// <returns></returns>
		public static async Task<List<Server_EventLogData>> GetQueryEventLogPage(string source, string event_type, string device_id, string start_time = "", string end_time = "")
		{
			string path = "/Query";
			var param = new
			{
				type = "event",
				source,
				event_type,
				device_id,
				start_time,
				end_time
			};
			var res = await PostData<Server_LogQuery<List<Server_EventLogData>>>(path, param);
			if (res == null || res.result == null || !res.result.Any())
			{
				return new List<Server_EventLogData>();
			}
			return res.result;
		}
		/// <summary>
		/// 查询获取数据中心
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="path"></param>
		/// <param name="param"></param>
		/// <returns></returns>
		public static async Task<T> PostData<T>(string path, dynamic param = null)
		{
			Server_Result<T> res = await PostRes<T>(path, param);
			if (res.msg_id != 0)
			{
				throw new Exception(res.msg);
			}
			return res.data;
		}

		/// <summary>
		/// 查询获取数据中心
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="path"></param>
		/// <returns></returns>
		public static async Task<Server_Result<T>> PostRes<T>(string path, dynamic param = null)
		{
			if (string.IsNullOrEmpty(Server))
			{
				throw new Exception("AVC日志服务");
			}
			string url = $"{Server}{path}";
			string paramStr = JsonConvert.SerializeObject(param);
			try
			{
				string jsonRes = await HTTPHelper.HttpPost(url, paramStr);
				Server_Result<T> result = JsonConvert.DeserializeObject<Server_Result<T>>(jsonRes) ?? new Server_Result<T> { msg_id = -1010, msg = "AVC日志服务请求错误" };
				return result;
			}
			catch (Exception ex)
			{
				string errMsg = ex.Message;
				Log.WriteException(new Exception($"AVC日志服务请求错误：path:{path}, param: {JsonConvert.SerializeObject(param)}, msg: {ex.Message}"));
				if (errMsg.Contains("无法连接"))
				{
					errMsg = "AVC日志服务连接失败，请联系管理员检查服务状态";
				}
				return new Server_Result<T> { msg_id = -1010, msg = errMsg };
			}
		}
	}
}
